package com.example.redisjson.controller;

import com.example.redisjson.dto.User;
import com.example.redisjson.repositories.json.UserRepository;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/api/users")
public class UserController {

    @Resource
    private UserRepository userRepository;

    @PostMapping("/")
    public User save(@RequestBody User user) {
        return userRepository.save(user);
    }

    @GetMapping("/q")
    public List<User> findByName(@RequestParam String firstName, @RequestParam String lastName) {
        return userRepository.findByFirstNameAndLastName(firstName, lastName);
    }

    @GetMapping("/name/{lastName}")
    Optional<User> byName(@PathVariable("lastName") String lastName) {
        return userRepository.findOneByLastName(lastName);
    }

    @GetMapping("/exists")
    boolean isEmailTaken(@RequestParam("email") String email) {
        return userRepository.existsByEmail(email);
    }

}